package com.af.monitorModule.controller.robot;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Robot;
import com.af.monitorModule.generator.service.RobotService;
import com.af.monitorModule.util.RestUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/records")
public class RecordsController {


    @Autowired
    private RobotService robotService;

    @PostMapping("/startService")
    public JSONObject startService(@RequestBody JSONObject data){
        String id = data.getString("id");
        String type = data.getString("type");
        String name = data.getString("name");
        Robot robot = robotService.getById(id);
        if (robot == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        System.out.println(robot.getRobotRequest() + "/service/startService/" + type + "/" + name);
        return RestUtil.sendPost(robot.getRobotRequest() + "/service/startService/" + type + "/" + name,JSONObject.class,new JSONObject());
    }
    @PostMapping("/stopService")
    public JSONObject stopService(@RequestBody JSONObject data){
        String id = data.getString("id");
        String type = data.getString("type");
        String name = data.getString("name");
        Robot robot = robotService.getById(id);
        if (robot == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        return RestUtil.sendPost(robot.getRobotRequest() + "/service/stopService/" + type + "/" + name,JSONObject.class,new JSONObject());
    }

    @PostMapping("/showInner/{robotType}/{robotName}/{id}")
    public JSONObject getInner(@RequestBody JSONObject data,@PathVariable String robotType,@PathVariable String robotName,@PathVariable String id){
        Robot robot = robotService.getById(id);
        if (robot == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        return RestUtil.sendPost(robot.getRobotRequest() + "/catalogue/show/"+ robotType + "/" + robotName,JSONObject.class,data);
    }

    @PostMapping("/downFile/{robotType}/{robotName}/{id}")
    public void downLoad(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject data, @PathVariable String robotType, @PathVariable String robotName, @PathVariable String id) throws IOException {
        Robot robot = robotService.getById(id);
        if (robot == null){
            response.setStatus(500);
            response.getOutputStream().print("数据不存在");
            return;
        }
        byte[] o = RestUtil.sendPost(robot.getRobotRequest() + "/catalogue/down/" + robotType + "/" + robotName, byte[].class, data);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(o);
        toClient.flush();
        toClient.close();
        System.gc();
    }
}
